package com.ayq.activiti.manager.controller;

import com.ayq.activiti.common.util.Ret;
import com.ayq.activiti.manager.domain.workflow.service.WorkFlowService;
import com.ayq.activiti.manager.dto.*;
import com.ayq.activiti.manager.service.RabbitMqTaskService;
import com.ayq.activiti.manager.vo.ActivitiNodeInfoVO;
import com.ayq.activiti.manager.vo.ApprovalecordVO;
import com.ayq.activiti.manager.vo.WorkFlowApplyListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>工作流</p>
 *
 * @Author ayq
 * @Date 2021/3/31 14:57
 */
@Controller
@RequestMapping("/work-flow")
@Api(tags = "工作流中心")
@Slf4j
public class WorkFlowController{

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkFlowController.class);

    @Autowired
    private WorkFlowService workFlowService;
    @Autowired
    private RabbitMqTaskService rabbitMqTaskService;

    @PostMapping("/startWorkFlow")
    @ApiOperation(value = "启动流程", notes = "启动流程")
    @ResponseBody
    public Ret startWorkFlow(@RequestBody StartWorkFlowDTO startWorkFlowDTO) {
        try {
            workFlowService.startWorkFlow(startWorkFlowDTO);
            return Ret.ok("流程启动成功");
        } catch (Exception e) {
            LOGGER.error("流程启动异常", e);
            return Ret.error(e.getMessage());
        }
    }

    /**
     * <p>获取用户审核列表</p>
     * <p>方法名:getUserTaskList</p>
     *
     * @Author ayq
     * @Date 2021/4/1 10:48
     * @Param [userId, workFlowKey]
     * @Return java.util.List<java.lang.String>
     */
    @GetMapping("/getUserTaskList")
    @ApiOperation(value = "获取用户审核列表", notes = "获取用户审核列表")
    @ResponseBody
    public Ret<List<WorkFlowApplyListVO>> getUserTaskList(UserTaskListDTO userTaskListDTO) {
        try {
            return Ret.ok(workFlowService.getUserTaskList(userTaskListDTO));
        } catch (Exception e) {
            LOGGER.error("获取用户审核列表异常", e);
            return Ret.error(e.getMessage());
        }
    }

    @GetMapping("/getUserApplyList")
    @ApiOperation(value = "获取用户申请列表", notes = "获取用户申请列表")
    @ResponseBody
    public Ret<List<WorkFlowApplyListVO>> getUserApplyList(UserApplyListDTO userApplyListDTO) {
        try {
            return Ret.ok(workFlowService.getUserApplyList(userApplyListDTO));
        } catch (Exception e) {
            LOGGER.error("获取用户申请列表异常", e);
            return Ret.error(e.getMessage());
        }
    }

    /**
     * <p>同意审批流程</p>
     * <p>方法名:agreeApprovalProcess</p>
     *
     * @Author ayq
     * @Date 2021/4/1 16:12
     * @Param [userId, businessKey, opinion, imgPath]
     * @Return org.ezca.esign.common.base.Ret
     */
    @PostMapping("/agreeApprovalProcess")
    @ApiOperation(value = "同意审批流程", notes = "同意审批流程")
    @ResponseBody
    public Ret<WorkFlowApplyListVO> agreeApprovalProcess(@RequestBody WorkFlowOpinionDTO workFlowOpinionDTO) {
        try {
            return Ret.ok(workFlowService.agreeApprovalProcess(workFlowOpinionDTO));
        } catch (Exception e) {
            LOGGER.error("同意审批流程异常", e);
            return Ret.error(e.getMessage());
        }
    }

    /**
     * <p>拒绝审批流程</p>
     * <p>方法名:refuseApprovalProcess</p>
     *
     * @Author ayq
     * @Date 2021/4/2 11:10
     * @Param [userId, businessKey, opinion]
     * @Return void
     */
    @PostMapping("/refuseApprovalProcess")
    @ApiOperation(value = "拒绝审批流程", notes = "拒绝审批流程")
    @ResponseBody
    public Ret refuseApprovalProcess(@RequestBody WorkFlowOpinionDTO workFlowOpinionDTO) {
        try {
            workFlowService.refuseApprovalProcess(workFlowOpinionDTO);
            return Ret.ok("拒绝审批流程成功");
        } catch (Exception e) {
            LOGGER.error("拒绝审批流程异常", e);
            return Ret.error(e.getMessage());
        }
    }


    @RequestMapping("/getHtml")
    public String getHtml() {
        return "model/statr";
    }


    /**
     * <p>获取业务历史审核意见</p>
     * <p>方法名:getTaskOpinionList</p>
     *
     * @Author ayq
     * @Date 2021/4/6 15:46
     * @Param [workFlowOpinionDTO]
     * @Return org.ezca.esign.common.base.Ret<java.util.List < org.ezca.esign.interfaces.api.activiti.dto.ApprovalecordDTO>>
     */
    @GetMapping("/getTaskOpinionList")
    @ApiOperation(value = "获取业务历史审核意见", notes = "获取业务历史审核意见")
    @ResponseBody
    public Ret<List<ApprovalecordVO>> getTaskOpinionList(WorkFlowOpinionDTO workFlowOpinionDTO) {
        try {
            return Ret.ok(workFlowService.getTaskOpinionList(workFlowOpinionDTO));
        } catch (Exception e) {
            LOGGER.error("获取业务历史审核意见异常", e);
            return Ret.error(e.getMessage());
        }
    }

    /**
     *
     * <p>根据任务ID获取业务历史审核意见</p>
     * <p>方法名:getTaskOpinionToTaskId</p>
     * @Author ayq
     * @Date 2021/4/19 11:12
     * @Param [taskId]
     * @Return org.ezca.esign.interfaces.api.activiti.vo.ApprovalecordVO
     */
    @GetMapping("/getTaskOpinionToTaskId/{taskId}")
    @ApiOperation(value = "根据任务ID获取业务历史审核意见", notes = "根据任务ID获取业务历史审核意见")
    @ResponseBody
    public Ret<ApprovalecordVO> getTaskOpinionToTaskId(@PathVariable("taskId") String taskId, @RequestParam("businessKey") String businessKey) {
        try {
            return Ret.ok(workFlowService.getTaskOpinionToTaskId(taskId,businessKey));
        } catch (Exception e) {
            LOGGER.error("根据任务ID获取业务历史审核意见异常", e);
            return Ret.error(e.getMessage());
        }
    }


    /**
     * <p>校验是否有流程在运行中</p>
     * <p>方法名:checkActivitiEnd</p>
     *
     * @Author ayq
     * @Date 2021/4/8 17:12
     * @Param [checkActivitiEndDTO]
     * @Return org.ezca.esign.common.base.Ret<org.ezca.esign.interfaces.api.activiti.dto.CheckActivitiEndDTO>
     */
    @GetMapping("/checkActivitiEnd")
    @ApiOperation(value = "校验是否有流程在运行中", notes = "校验是否有流程在运行中")
    @ResponseBody
    public Ret<CheckActivitiEndDTO> checkActivitiEnd(CheckActivitiEndDTO checkActivitiEndDTO) {
        try {
            return Ret.ok(workFlowService.checkActivitiEnd(checkActivitiEndDTO));
        } catch (Exception e) {
            LOGGER.error("校验是否有流程在运行中异常", e);
            return Ret.error(e.getMessage());
        }
    }

    /**
     *
     * <p>获取节点信息</p>
     * <p>方法名:getNodeInfo</p>
     * @Author ayq
     * @Date 2021/4/16 14:47
     * @Param [nodeInfoDTO]
     * @Return org.ezca.esign.common.base.Ret<org.ezca.esign.interfaces.api.activiti.vo.ActivitiNodeInfoVO>
     */
    @GetMapping("/getNodeInfo")
    @ApiOperation(value = "获取节点信息", notes = "获取节点信息")
    @ResponseBody
    public Ret<ActivitiNodeInfoVO> getNodeInfo(NodeInfoDTO nodeInfoDTO) {
        try {
            return Ret.ok(workFlowService.getNodeInfo(nodeInfoDTO));
        } catch (Exception e) {
            LOGGER.error("获取节点信息异常", e);
            return Ret.error(e.getMessage());
        }
    }

    @RequestMapping("/getImgHtml")
    public String getImgHtml(String processInstanceId, Model model) {
        model.addAttribute("processInstanceId",processInstanceId);
        return "model/img";
    }

    /**
     *
     * <p>根据实例ID查看流程图</p>
     * <p>方法名:getProcessInstanceIdBpmnImgs</p>
     * @Author ayq
     * @Date 2021/4/16 15:17
     * @Param [processInstanceId, resp]
     * @Return void
     */
    @ApiOperation(value = "根据实例ID查看流程图", notes = "根据部署ID查看流程图")
    @GetMapping("/getProcessInstanceIdIdBpmnImgs/{processInstanceId}")
    public void getProcessInstanceIdBpmnImgs(@PathVariable("processInstanceId") String processInstanceId, HttpServletResponse resp) {
        try {
            workFlowService.getProcessInstanceIdBpmnImgs(processInstanceId,resp);
        } catch (Exception e) {
            LOGGER.error("processInstanceId:" + processInstanceId + "生成流程图失败", e);
        }
    }


    @GetMapping("/activitiNodeInfoUpdate/{processInstanceId}")
    public Ret activitiNodeInfoUpdate(@PathVariable("processInstanceId") String processInstanceId) {
        try {
            rabbitMqTaskService.updateNodeInfo(processInstanceId);
            return Ret.ok("更新成功");
        } catch (Exception e) {
            LOGGER.error("processInstanceId:" + processInstanceId + "更新失败", e);
            return Ret.error("更新失败");
        }
    }

}
